import { action, observable } from 'mobx';
import Base from './base';

export default class CommonState extends Base {

    constructor(props) {
        super(props);
        // 注入其它需要调用的store
        setTimeout(() => {
            const { projectState, taskState } = props;
            this.projectState = projectState;
            this.taskState = taskState;
        })

    }

    @observable initStatus = 'connecting';
    @observable projectModal = false;
    @observable taskModal = false;
    @observable contactModal = false;
    projecttype = 1;
    projectname = undefined;
    baseData = undefined;
    @observable sideOpen = false;



    @action.bound
    async setStatus(val) {
        this.initStatus = val;
    }

    @action.bound
    async onCreateProjectModalChange({ projectModal, projecttype, projectname }) {
        this.projecttype = projecttype;
        this.projectname = projectname;
        this.projectModal = projectModal;
    }


    @action.bound
    async onCreateTaskModalChange({ taskModal, baseData }) {
        // 由于添加和详情页面都有TaskMember组件，并且里面都做了主线程事件的监听，所以要定义一个全局的变量控制不同的情况
        window.isCreate = taskModal;

        if (this.baseData && !taskModal) { // 项目详情创建任务成功关闭modal刷新项目详情
            this.projectState.getDetail({ projectid: this.projectState.detailData.projectid }, true);
        }

        this.baseData = baseData;
        this.taskModal = taskModal;
    }

    @action.bound
    async onCreateContactModalChange({ contactModal }) {
        this.contactModal = contactModal;
    }

    @action.bound
    refreshProjectDetail() {
        if (this.taskState.needRefreshProjectDetail) {
            this.projectState.getDetail({ projectid: this.projectState.detailData.projectid }, true);
        }
    }

}
